package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.controller.vo;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ExcelVO implements CodeGenTemplate {

    @Autowired
    CommonColumn commonColumn;

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplVOFilePath("ExcelVO");
    }


    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.bl()
                .apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ".vo;")
                .bl()
                .apAndBl("import lombok.*;")
                .apAndBl("import java.math.BigDecimal;")
                .apAndBl("import java.util.*;")
                .apAndBl("import io.swagger.annotations.*;")
                .apAndBl("import javax.validation.constraints.*;")
                .bl()
                .apAndBl("import com.alibaba.excel.annotation.ExcelProperty;")
        ;
        for (XiaoBizHandlerCodegenColumnDO xiaoBizHandlerCodegenColumnDO : columns) {
            // 有设置数据字典
            if (xiaoBizHandlerCodegenColumnDO.getDictType() != null && !"".equals(xiaoBizHandlerCodegenColumnDO.getDictType())) {
                code.apAndBl("import " + globalBinding.getDictFormatClassName() + ";");
                code.apAndBl("import " + globalBinding.getDictConvertClassName() + ";");
                code.bl();
                break;
            }
        }

        code
                .bl()
                .apAndBl("/**")
                .apAndBl(" * " + table.getClassComment() + " Excel VO")
                .apAndBl(" *")
                .apAndBl(" * @author " + table.getAuthor() + "")
                .apAndBl(" */")
                .apAndBl("@Data")
                .apAndBl("public class " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "ExcelVO {")
                .bl()
        ;

        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            // 返回字段
            if (column.getListOperationResult()) {
                // 处理枚举值
                if (column.getDictType() != null && !"".equals(column.getDictType())) {
                    code
                            .apAndBl("    @ExcelProperty(value = \"" + column.getColumnComment() + "\", converter = DictConvert.class)")
                            .apAndBl("    @DictFormat(\"" + column.getDictType() + "\") // TODO 代码优化：建议设置到对应的 XXXDictTypeConstants 枚举类中")
                    ;
                } else {
                    code
                            .apAndBl("    @ExcelProperty(\"" + column.getColumnComment() + "\")")
                    ;
                }
                code.apAndBl("    private " + column.getJavaType() + " " + column.getJavaField() + ";");
                code.bl();
            }
        }
        code.apAndBl("}");
        code.bl();
        return code.toString();
    }

}